<template>
	<view>
		<view>
			<u-navbar   :is-back="true" title="申请添加好友"
						:title-size="36"  
						:background="{ background: '#ffffff'}"
						title-color="#323232" 
						:border-bottom="false">
			</u-navbar>
		</view>
		<view>
			<view class="u-m-t-30 u-p-l-40 u-p-r-40">
				<view class="u-tips-color u-font-32">
					<text>你需要发送验证申请，等待对方通过</text>
				</view>
				<view  class="inputBox">
					<u-input :placeholder="usuallyTip"  :custom-style="customInputStyle" 
							placeholder-style="font-size:32rpx" @focus="friendNameFocus()"
							v-model="form.applyUserName" type="textarea" :border="false" 
							:height="200"
							:clearable="vuex_OSPlat=='android'?false:true"/>
				</view>
			</view>
			
			<view class="u-m-t-50 u-p-l-40 u-p-r-40">
				<view class="u-tips-color u-font-32">
					<text>设置备注</text>
				</view>
				<view  class="inputBox">
					<u-input :placeholder="friendNameTip"  :custom-style="customInputStyle" 
							placeholder-style="font-size:32rpx" @focus="friendNameFocus()"
							v-model="form.friendName" type="textarea" :border="false" 
							:clearable="vuex_OSPlat=='android'?false:true"/>
				</view>
			</view>
		</view>
		
		<view  	class="u-flex u-row-center u-col-top footerbox">
			<u-button type="success" :custom-style="customBtnStyle" @click="submitApply">发送</u-button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				backPageDelta:2,
				friendNameTip:'',
				usuallyTip:'',
				customInputStyle:{
					fontSize:'32rpx',
					color:'#000000'	
				},
				customBtnStyle:{
					padding:'20rpx 40rpx',
					width:'300rpx'
				},
				newFriendInfo:{
					
				},
				form:{
					applyUserId:'',
					applyUserName: '',
					friendName:'',
					friendTag:'',
					friendPower:0,
					forbidSelf:false,
					forbidHis:false
				},
			};
		},
		onLoad:function(option){
			let that=this;
			let fromPage= option.fromPage;
			if(fromPage!=null&&fromPage!=undefined){
				that.backPageDelta=fromPage;
			};
			that.newFriendInfo=JSON.parse(decodeURIComponent(option.newFriendInfo));
			console.log("新的朋友",that.newFriendInfo);
			this.friendNameTip=that.newFriendInfo.userName;
			this.usuallyTip="我是【"+that.vuex_userInfo.userName+"】";
			this.form.applyUserId=that.vuex_userInfo.id;
			
		},
		methods:{
			friendNameFocus:function(){
				let that=this;
				if(this.form.friendName.length<1){
					this.form.friendName=that.newFriendInfo.userName;
					this.friendNameTip="";
				}
			},
			
			changePower:function(status){
				this.form.friendPower=status;
			},
			
			submitApply:function(){
				let that=this;
				let param={
					friendId:that.newFriendInfo.id,
					friendName:that.form.friendName.length<1?that.newFriendInfo.userName:that.form.friendName,
					friendTag:that.form.friendTag,
					friendMobile:that.newFriendInfo.mobile,
					friendPower:that.form.friendPower,
					description:that.form.applyUserName,
				}
				console.log("param",param);
				that.$u.api.imUser.addFriend(param).then(res => {
					if(res.code==200){
					   console.log("申请添加好友结果",res);
					   this.globalUtil.utilAlert("已发送好友申请");
					   //发送通知,提醒对方处理好友请求
					   this.sendFriendApplyNotice(param);
					   uni.navigateBack({
						   delta:3,
					   })
					}
				})
			},
			
		
			
			sendFriendApplyNotice:function(messageContent){
				let that=this;
				let timestamp=new Date().getTime();
				let message = {
					"id": new Date().getTime() + "",
					"chatGroupId": null,
					"userId": that.vuex_userInfo.id,
					"meFlag": true,
					"userName": that.vuex_userInfo.userName,
					//"userAvatar": that.vuex_userInfo.avatar, 
					//'messageType': this.messageApi.MSG_TYPE.SYSTEM_MSG,
					//"contentType": this.messageApi.CONTENT_TYPE.TEXT_CONTENT_TYPE,
					"content": {'text':'您有收到一条好友申请请求信息',param:messageContent},
					"createTime": that.globalUtil.getTimeStr(timestamp),
					"timestamp": timestamp,
					"platFrom": this.messageApi.msgPlat,
					"showTimeFlag": true,
				}
				console.log("发送新建群聊信息", message);
				this.globalWX.sendMessage(message, {
					complete: function(res) {
						console.log("=======已发送==========", res);
						if(res==true){
							
						}else{
							that.globalUtil.utilAlert("网络故障，稍后重试！");
							return;
						}
					}
				},that.messageApi.BUSINESS_CODE.FriendBindChange);
			},
			
		}
	}
</script>

<style lang="scss" scoped>
	.inputBox{
		background-color: #F2F2F7;
		border-radius: 16rpx;
		margin-top: 20rpx;
		padding:16rpx 26rpx;
		font-size: 32rpx;
	}
	.footerbox{
		position: absolute;
		bottom: 40px;
		width: 100%;
		background-color: #ffffff;
	}
</style>
